package com.mb.app.controller;

import com.mb.app.domain.TbProductInfo;
import com.mb.app.domain.TbSpecialClass;
import com.mb.app.service.ITbSpecialClassService;
import com.mb.common.utils.DateUtils;
import com.mb.common.utils.PageUtils;
import com.mb.common.utils.Query;
import com.mb.common.utils.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 专题分类  前端控制器
 * </p>
 *
 * @author liliping
 * @since 2019-04-27
 */
@Controller
@RequestMapping("/app/specialClass")
public class TbSpecialClassController {

    @Autowired
    private ITbSpecialClassService specialClassService;

    @GetMapping("/class")
    String index(){
        return "/app/specialClass/special_listClass";
    }

    /**
     * 专题分类信息
     * @param params
     * @return
     */
    @ResponseBody
    @GetMapping("/list")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        // 时间截取
        String createTime = (String) params.get("createTime");
        if (StringUtils.isNotEmpty(createTime)) {
            params.put("startTime", DateUtils.parse(createTime.substring(0,10),DateUtils.DATE_PATTERN));
            params.put("endTime",createTime.substring(13,23)+" 23:59:59");
        }
        Query query = new Query(params);
        List<TbProductInfo> list = specialClassService.queryList(query);
        int total = specialClassService.count(query);
        PageUtils pageUtils = new PageUtils(list, total);
        return pageUtils;
    }

    /**
     * 跳转添加专题分类页面
     * @param
     * @return
     */
    @GetMapping("/add")
    String add(){
        return "/app/specialClass/special_addClass";
    }

    /**
     * 添加
     * @param specialClass
     * @return
     */
    @ResponseBody
    @PostMapping("/save")
    public R save(TbSpecialClass specialClass){
        boolean insert = specialClassService.insert(specialClass);
        if (insert){
            return R.ok();
        }
        return R.error("添加失败");
    }

    /**
     * 修改
     * @param specialClass
     * @return
     */
    @ResponseBody
    @PostMapping("/update")
    public R update(TbSpecialClass specialClass){
        boolean b = specialClassService.updateById(specialClass);
        if (b){
            return R.ok();
        }
        return R.error("修改失败");
    }

    /**
     * 修改
     * @param specialClassId
     * @param model
     * @return
     */
    @GetMapping("/edit/{specialClassId}")
    String edit(@PathVariable("specialClassId") String  specialClassId, Model model){
        TbSpecialClass tbSpecialClass = specialClassService.selectById(specialClassId);
        model.addAttribute("TbSpecialClass",tbSpecialClass);
        return "/app/specialClass/special_editClass";
    }


    /**
     * 删除
     * @param specialClassId
     * @param request
     * @return
     */
    @ResponseBody
    @PostMapping("/remove")
    public R remove(@RequestParam("specialClassId") Long specialClassId, HttpServletRequest request){
        TbSpecialClass specialClasses = new TbSpecialClass();
        specialClasses.setSpecialClassId(specialClassId);
        specialClasses.setDataStatus(0);
        boolean b = specialClassService.updateById(specialClasses);
        if (b){
            return R.ok();
        }
        return R.error("删除失败");
    }
}
